<?php

namespace App\Controllers;

use App\Services\GameService;
use App\Services\GameTagService;
use App\Services\PartnerService;
use Illuminate\Http\Request;

class GameController extends BaseController
{
    /**
     *  删除API活动状态
    */
    public function deleteApiActive(Request $request)
    {
        $params = $this->validate($request, [
            'id' => 'required'
        ]);
        return GameService::deleteApiActive($params['id']);
    }

    /**
     *  修改API活动状态
    */
    public function updateApiActiveStatus(Request $request)
    {
        $params = $this->validate($request, [
            'id' => 'required',//活动id
            'status' => 'required',//活动名称
        ]);
        return GameService::updateApiActiveStatus($params);
    }

    /**
     *  获取API活动
    */
    public function getGameActive(Request $request)
    {
        return GameService::getGameActive($request->all());
    }

    /**
     *  API活动-获取活动类型
    */
    public function getGameActiveType(Request $request)
    {
        return GameService::getGameActiveType();
    }

    /**
     * 修改API活动
    */
    public function updateApiActive(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',//活动id
            'title' => 'required',//活动名称
            'type' => 'required',//游戏类型
            'img_pc' => 'required',//pc端图片
            'begin_time' => 'required',
            'end_time' => 'required',
            'status' => 'required',
        ]);
        $params = GameService::checkParams($request->all());
        return GameService::updateApiActive($params);
    }

    /**
     * 新增API活动
    */
    public function addApiActive(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',//活动名称
            'type' => 'required',//游戏类型
            'begin_time' => 'required',
            'end_time' => 'required',
            'status' => 'required',
        ]);
        $params = GameService::checkParams($request->all());
        return GameService::addApiActive($params);
    }

    public function getPartner(Request $request)
    {
        $gameType = $request->input('game_type');
        return PartnerService::getPartner($gameType);
    }

    public function getPartnerSetting(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ]);
        $id = $request->input('id');
        return PartnerService::getPartnerSetting($id);
    }

    public function updatePartnerSetting(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'config' => 'required',
        ]);
        $id = $request->input('id');
        $config = $request->input('config');
        return PartnerService::updatePartnerSetting($id, $config);
    }

    public function getPartnerConfig(Request $request)
    {
        return PartnerService::getPartnerConfig();
    }

    public function getHotSettingList(Request $request)
    {
        $gameType = $request->input('game_type', false);
        return PartnerService::getHotSettingList($gameType);
    }

    public function updateHotSetting(Request $request)
    {
        $this->validate($request, [
            'game_type' => 'required',
            'list' => 'required|array'
        ]);
        $gameType = $request->input('game_type');
        $list = $request->input('list');
        return PartnerService::updateHotSetting($gameType, $list);
    }

    public function updatePartnerConfig(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ]);
        $id = $request->input('id');
        $arrts = [];
        if ($request->has('status')) {
            $arrts['status'] = $request->input('status');
        }
        if ($request->has('maintaining')) {
            $arrts['maintaining'] = $request->input('maintaining');
        }
        return PartnerService::updatePartnerConfig($id, $arrts);
    }

    public function getTagList(Request $request)
    {
        $this->validate($request, [
            'game_type' => 'required',
        ]);
        $gameType = $request->input('game_type');
        return GameTagService::getTagList($gameType);
    }

    public function addTag(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'pid' => 'required',
            'game_type' => 'required',
        ]);
        $name = $request->input('name');
        $pid = $request->input('pid');
        $gameType = $request->input('game_type');
        return GameTagService::addTag($name, $pid, $gameType) ? 1 : 0;
    }

    public function delTag(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ]);
        $id = $request->input('id');
        return GameTagService::delTag($id);
    }

    public function updateTag(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'name' => 'required',
        ]);
        $id = $request->input('id');
        $name = $request->input('name');
        return GameTagService::updateTag($id, $name);
    }

    public function getGameList(Request $request)
    {
        $params = $request->only(['game_type', 'partner_id', 'tag_id', 'game_name']);
        $page = $request->input('page', 1);
        $size = $request->input('page_size', 20);
        return GameService::getGameList($params, $page, $size);
    }

    public function updateGame(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ]);
        $id = $request->input('id');
        $arrts = [];
        if ($request->has('new_end_time')) {
            $arrts['new_end_time'] = $request->input('new_end_time');
        }
        if ($request->has('hot_end_time')) {
            $arrts['hot_end_time'] = $request->input('hot_end_time');
        }
        if ($request->has('badge')) {
            $arrts['badge'] = $request->input('badge');
        }
        return GameService::updateGame($id, $arrts);
    }

    public function updateGameTag(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'tags' => 'required|array',
        ]);
        $id = $request->input('id');
        $tags = $request->input('tags');
        return GameService::updateGameTag($id, $tags);
    }


    public function getGameRecommend(Request $request)
    {
        $params = $request->only(['game_type', 'partner_id', 'status']);
        $page = $request->input('page', 1);
        $size = $request->input('page_size', 20);
        return GameService::getGameRecommend($params, $page, $size);
    }

    public function updateGameRecommend(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ]);
        $id = $request->input('id');
        $arrts = [];
        if ($request->has('start_time')) {
            $arrts['start_time'] = $request->input('start_time');
        }
        if ($request->has('end_time')) {
            $arrts['end_time'] = $request->input('end_time');
        }
        if ($request->has('status')) {
            $arrts['status'] = $request->input('status');
        }
        if ($request->has('sort')) {
            $arrts['sort'] = $request->input('sort');
        }
        return GameService::updateGameRecommend($id, $arrts);
    }

    public function addGameRecommend(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required|array',
            'start_time' => 'required',
            'end_time' => 'required',
            'status' => 'required',
        ]);
        $ids = $request->input('ids');
        $startTime = $request->input('start_time');
        $endTime = $request->input('end_time');
        $status = $request->input('status');
        $remark = $request->input('remark', "");
        return GameService::addGameRecommend($ids, $startTime, $endTime, $status, $remark);
    }

    public function delGameRecommend(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
        ]);
        $id = $request->input('id');
        return GameService::delGameRecommend($id);
    }

    public function getPartnerList(Request $request)
    {
        return PartnerService::getPartnerList();
    }

    public function setMaintainTime(Request $request)
    {
        $this->validate($request, [
            'partner_id' => 'required',
            'game_type' => 'required',
        ]);
        $arrts = $request->input();
        if (!empty($arrts['end_time']) && !empty($arrts['start_time'])) {//时间为空设置成null
            if (time() > strtotime($arrts['end_time'])) {//当前时间大于结束时间
                error_exit(SysCode::GAME_API_END_TIME_ERROR);
            }
            if (strtotime($arrts['start_time']) > strtotime($arrts['end_time'])) {//开始时间大于结束时间
                error_exit(SysCode::GAME_API_START_TIME_ERROR);
            }
        } else {
            $arrts['end_time']   = null;
            $arrts['start_time'] = null;
        }
        return PartnerService::setMaintainTime($arrts);
    }
}
